package com.itheima.youdianda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.common.PageBean;
import com.itheima.youdianda.common.ResultData;
import com.itheima.youdianda.dto.YddArticleDto;
import com.itheima.youdianda.entity.YddArticle;
import com.itheima.youdianda.service.YddArticleService;
import com.itheima.youdianda.service.YddCateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_article")
@Slf4j
public class YddArticleController {

    @Autowired
    private YddArticleService yddArticleService;

    @Autowired
    private YddCateService yddCateService;

    /**
     * 文章分页查询
     * @param start_time
     * @param end_time
     * @param page
     * @param limit
     * @param title
     * @param cateid
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String start_time,
                  String end_time,
                  int page,
                  int limit,
                  String title,
                  Integer cateid) {
        log.info("传来的值为:{}{}{}{}{}{}", start_time, end_time, page, limit, title, cateid);
        log.info("传来的值为:{}", cateid);
        //创建分页查询器
        Page<YddArticle> pageInfo = new Page<>(page, limit);
        Page<YddArticleDto> yddArticleDtoPage = new Page<>();
        //创建构造器
        LambdaQueryWrapper<YddArticle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(cateid != null, YddArticle::getCateid, cateid);
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(title), YddArticle::getTitle, title);
        lambdaQueryWrapper.between(StringUtils.isNotEmpty(start_time) && StringUtils.isNotEmpty(end_time), YddArticle::getCreateDate, start_time, end_time);
        lambdaQueryWrapper.orderByDesc(YddArticle::getCreateDate);
        Page<YddArticle> yddArticlePage = yddArticleService.page(pageInfo, lambdaQueryWrapper);
        //对象的拷贝
        BeanUtils.copyProperties(yddArticlePage, yddArticleDtoPage,"records");
        List<YddArticle> records = yddArticlePage.getRecords();

        ArrayList<YddArticleDto> yddArticleDtos = new ArrayList<>();

        for (YddArticle record : records) {
            YddArticleDto yddArticleDto = new YddArticleDto();
            BeanUtils.copyProperties(record,yddArticleDto);
            Integer yddCateId = record.getCateid();
            String catename = yddCateService.getById(yddCateId).getCatename();
            yddArticleDto.setCatename(catename);
            yddArticleDtos.add(yddArticleDto);
        }
        //以下是返回的数据 page类型
        yddArticleDtoPage.setRecords(yddArticleDtos);
        //定义一个pageBean的返回对象
        PageBean<YddArticleDto> pageBean = new PageBean<>();
        pageBean.setCount((int)yddArticleDtoPage.getTotal());
        pageBean.setCurrentPage((int)yddArticleDtoPage.getCurrent());
        pageBean.setPageSize((int)yddArticleDtoPage.getSize());
        pageBean.setTotalPages((int)yddArticleDtoPage.getPages());
        pageBean.setData(yddArticleDtoPage.getRecords());

        return ResultData.ok("查询成功", pageBean);
    }

    /**
     * 文章删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(String id){
        log.info("传入的数据为: {}",id);
        yddArticleService.removeById(id);
        return ResultData.ok("","");
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(String id){
        //执行业务层操作
        log.info("数据为:{}",id);

        YddArticle article = yddArticleService.getById(id);
        return  ResultData.ok("",article);
    }

    /**
     * 文章新增
     * @param yddArticle
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddArticle yddArticle){
        log.info("传来的数据为: {}",yddArticle);
        yddArticleService.save(yddArticle);
        return ResultData.ok("新增成功",yddArticle);
    }

    /**
     * 文章修改
     * @param yddArticle
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddArticle yddArticle){
        yddArticleService.saveOrUpdate(yddArticle);
        return ResultData.ok("修改成功",yddArticle);
    }
}
